<?php

namespace App\Http\Controllers;

use App\Http\Controllers\api\BaseController;
use App\logic\PhoneLogic;
use App\Models\users;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    /**
     * 静默登录
     * @name lzh
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function Index(Request $request){
        $code=$request->get('code');
        try {
            $result=PhoneLogic::Logins($code);
            return (new BaseController())->witejson(0,$result,'登录成功',200);
        }catch (\Exception $exception){
            return (new BaseController())->witejson(1,[],$exception->getMessage(),500);
        }
    }

    /**
     * 发送短信
     * @name lzh
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function Phones(Request $request){
        $phone=$request->get('phone');
        try {
            $result=PhoneLogic::Phones($phone);
            return (new BaseController())->witejson(0,$result,'短信发送成功',200);
        }catch (\Exception $exception){
            return (new BaseController())->witejson(1,[],$exception->getMessage(),500);
        }
    }

    /**
     * 绑定手机号
     * @name lzh
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function doLogin(Request $request){
        $phone=$request->post('phone');
        $code=$request->post('code');
        $user_id=$request->post('user_id');
        try {
            $result=PhoneLogic::PhoneLogins($phone,$code,$user_id);
            return (new BaseController())->witejson(0,$result,'手机号绑定成功',200);
        }catch (\Exception $exception){
            return (new BaseController())->witejson(1,[],$exception->getMessage(),500);
        }
    }
}
